home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 February / EnigmA AMIGA RUN 15 (1997)(G.R. Edizioni)(IT)[!][issue 1997-02][PLANET CD V].iso / progs / editor / frexxed / fpl / tetris.fpl < prev    next >
Text File  |  1995-08-06  |  5KB  |  254 lines

  1.  
  2. string stars="OOOO";
  3.  
  4. string s_figure[4*4*5]={
  5.  "    ", "**  ", "**  ", "    ",     // block
  6.  "    ", "**  ", "**  ", "    ", 
  7.  "    ", "**  ", "**  ", "    ", 
  8.  "    ", "**  ", "**  ", "    ", 
  9.  
  10.  "    ", "*** ", " *  ", "    ",    // hake
  11.  " *  ", " ** ", " *  ", "    ",
  12.  " *  ", "*** ", "    ", "    ",
  13.  " *  ", "**  ", " *  ", "    ",
  14.  
  15.  "    ", "****", "    ", "    ",    // stake
  16.  " *  ", " *  ", " *  ", " *  ",
  17.  "    ", "****", "    ", "    ",
  18.  " *  ", " *  ", " *  ", " *  ",
  19.  
  20.  "    ", "*** ", "*   ", "    ",    // pinne-v
  21.  "    ", " *  ", " *  ", " ** ",
  22.  "    ", "  * ", "*** ", "    ",
  23.  "    ", " ** ", "  * ", "  * ",
  24.  
  25.  "    ", "*** ", "  * ", "    ",    // pinne-h
  26.  "    ", " ** ", " *  ", " *  ",
  27.  "    ", "*   ", "*** ", "    ",
  28.  "    ", "  * ", "  * ", " ** "
  29.  
  30. };
  31.                  //0 1 2 3 4 5 6 7 8 9 a b c d e f
  32. int fast_len[16] ={0,1,1,2,1,0,2,3,1,0,0,0,2,0,3,4};
  33. int fast_strt[16]={0,0,1,0,2,0,1,0,3,0,0,0,2,0,1,0};
  34.  
  35. int i_figure[4*4*5];
  36. int score;
  37.  
  38. string emptystr=" #          # ";
  39. string name;
  40.  
  41. int yard[24];
  42. string s_yard[24];
  43.  
  44. void init()
  45. {
  46.   int count1, count2;
  47.   int sum;
  48.  
  49.   for (count1=0; count1<4*4*5; count1++) {
  50.     sum=0;
  51.     for (count2=0; count2<4; count2++) {
  52.       sum=sum<<1;
  53.       if (s_figure[count1][count2]!=' ')
  54.         sum++;
  55.     }
  56.     i_figure[count1]=sum;
  57.   }
  58.  
  59. }
  60.  
  61. void update()
  62. {
  63.   int count, sum;
  64.   string str;
  65.   PrintLine(s_yard[1]=" /##########\\ ", 1);
  66.   for (count=2; count<19; count++) {
  67.     str=emptystr;
  68.     sum=yard[count];
  69.     if (sum&0xffc) {
  70.       if(sum&0x04) str[2]='*';
  71.       if(sum&0x08) str[3]='*';
  72.       if(sum&0x10) str[4]='*';
  73.       if(sum&0x20) str[5]='*';
  74.       if(sum&0x40) str[6]='*';
  75.       if(sum&0x80) str[7]='*';
  76.       if(sum&0x100)str[8]='*';
  77.       if(sum&0x200)str[9]='*';
  78.       if(sum&0x400)str[10]='*';
  79.       if(sum&0x800)str[11]='*';
  80.     }
  81.     PrintLine(str, count);
  82.     s_yard[count]=str;
  83.   }
  84.   PrintLine(" \\##########/ ", count);
  85.   PrintLine(sprintf(" score:%6d ",score), count+1);
  86. }
  87.  
  88.  
  89. void check()
  90. {
  91.   int count, move;
  92.   for (count=18; count>0; count--) {
  93.     if (yard[count]==0x1ffe) {
  94.       for(move=count; move>1; move--) {
  95.         yard[move]=yard[move-1];
  96.         s_yard[move]=s_yard[move-1];
  97.         score+=5;
  98.       }
  99.       yard[1]=0x1002;
  100.       s_yard[2]=emptystr;
  101.       count++;
  102.     }
  103.   }
  104.   update();
  105. }
  106.  
  107.  
  108. int test(int fig, int x, int y)
  109. {
  110.   int ret;
  111.   ret=(i_figure[fig]<<x)&yard[y];
  112.   ret|=(i_figure[fig+1]<<x)&yard[y+1];
  113.   ret|=(i_figure[fig+2]<<x)&yard[y+2];
  114.   ret|=(i_figure[fig+3]<<x)&yard[y+3];
  115.   return ret;
  116. }
  117.  
  118. void set(int fig, int x, int y)
  119. {
  120.   int count, sum;
  121.   string str;
  122.   PrintLine(s_yard[y-1], y-1);
  123.   for (count=0; count<4; count++) {
  124.     if (count+y<19) {
  125.       str=s_yard[count+y];
  126.       sum=i_figure[count+fig];
  127.       str=substr(str, 0, x+fast_strt[sum])+
  128.           substr(stars, 0, fast_len[sum])+
  129.           substr(str, x+fast_strt[sum]+fast_len[sum], -1);
  130.       PrintLine(str, y+count);
  131.     }
  132.   }
  133. }
  134.  
  135. int addhighscore()
  136. {
  137.   int id=New();
  138.   int oldid=GetEntryID();
  139.   int ret=0;
  140.  
  141.   if (id) {
  142.     CurrentBuffer(id);
  143.     System("makedir FrexxEd:FPL/highscore", "", "NIL:");
  144.     Load("FrexxEd:FPL/highscore/Tetris.score");
  145.     GotoLine(-1);
  146.     Output(sprintf("%8d - %s\n", score, name));
  147.     BlockSort(id,0,2);
  148.     while (ReadInfo("lines")>11) {
  149.       GotoLine(11);
  150.       DeleteLine();
  151.     }
  152.     Save();
  153.     ret=Request(sprintf("Your score: %d\n\n HIGHSCORES:\n%s",score,GetBlock(id)), "TETRIS HIGHSCORE", "Play again!|I give up!");
  154.     Clean("Kill();");
  155.     CurrentBuffer(oldid);
  156.   }
  157.   return ret;
  158. }
  159.  
  160.  
  161. {
  162.   int x=4, y=1, rot, symbol;
  163.   int temp;
  164.   int count;
  165.   string key;
  166.   int drop=0;
  167.   int loop;
  168.   int delay=0, dcount=0;
  169.  
  170.   delay=PromptInt("Delay", 0, "Enter delay of game");
  171.  
  172.   symbol=abs(Random()%5)*4*4;
  173.   rot=symbol;
  174.   CursorActive(0);
  175.   init();
  176.  
  177.   do {
  178.     for (count=0; count<19; count++)
  179.       yard[count]=0x1002;
  180.     yard[19]=-2;
  181.   
  182.     for (count=0; count<26; count++) {
  183.       PrintLine("#########################################", count);
  184.       GetKey(1);
  185.     }
  186.     score=0;
  187.     update();
  188.  
  189.     name=PromptString(name, "TETRIS", "Enter name:");
  190.     if (!strlen(name)) {
  191.       if (GetErrNo()<0) {
  192.         RedrawScreen();
  193.         return;
  194.       }
  195.       name="Unknown";
  196.     }
  197.     while (1) {
  198.       loop=3;
  199.       while (--loop && strlen(key=GetKey(1))) {
  200.         if (key[0]==0x9b) {
  201.           switch(key[1]) {
  202.           case 'A': //up
  203.             temp=((rot+4)&12)+symbol;
  204.             if (!test(temp, x, y))
  205.               rot=temp;
  206.             break;
  207.           case 'B': //down
  208.             temp=((rot-4)&12)+symbol;
  209.             if (!test(temp, x, y))
  210.               rot=temp;
  211.             break;
  212.           case 'C': //right
  213.             if (!test(rot, x+1, y))
  214.               x++;
  215.             break;
  216.           case 'D': //left
  217.             if (x>0 && !test(rot, x-1, y))
  218.               x--;
  219.             break;
  220.           }
  221.         } else if (key[0]==' ') {
  222.           while (!test(rot, x, ++y))
  223.             score+=3;
  224.           drop=1;
  225.           y--;
  226.         }
  227.       }
  228.       if (drop || test(rot, x, y+1)) {
  229.         score+=10;
  230.         for (count=0; count<4; count++)
  231.           yard[y+count]|=i_figure[count+rot]<<x;
  232.         symbol=abs(Random()%5)*4*4;
  233.         rot=symbol;
  234.         x=4;
  235.         y=1;
  236.         drop=0; 
  237.         check();
  238.         if (test(symbol, x, y))
  239.           break; //exit
  240.         set(rot, x, y);
  241.       } else {
  242.         if (++dcount>=delay) {
  243.           dcount=0;
  244.           y++;
  245.         }
  246.         set(rot, x, y);
  247.       }
  248.     }
  249.   } while (addhighscore());
  250.   RedrawScreen();
  251. }
  252.  
  253.  
  254.